//
//  FirstViewController.m
//  eyeinfo
//
//  Created by kingsoft on 11-1-6.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NewsViewController.h"
#import "eyeinfoAppDelegate.h"
#import "RssItem.h"
#import "GetRssDataLogical.h"
#import "NewWebView.h"
#import "GlobalCls.h"
#import "CustomNavigationBar.h"
#import "eyeinfo.h"

#define kScreenWidth 320
#define kScreenHeight 480

@implementation NewsViewController

@synthesize pageControl;
@synthesize tableViewPtr;

/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	self.title = @"IT新闻";
	
	m_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
	[m_spinner setCenter:CGPointMake(kScreenWidth/2.0, kScreenHeight/2.0)];
	[self.view addSubview:m_spinner];
	
	m_nCurPage = 1;
	[self refrashTableData:TRUE];
	
	
	//[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg.png"]];
}


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (tableViewPtr ==nil)
		self.tableViewPtr = tableView;
	eyeinfoAppDelegate* appDelegate = (eyeinfoAppDelegate*)[UIApplication sharedApplication].delegate;
	GetRssDataLogical* getRssDataLogic = appDelegate.m_getITRssLogic;
	NSLog(@"[appDelegate.rsslist count] %d", [getRssDataLogic.m_rsslist count]);
	return [getRssDataLogic.m_rsslist count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
	
	eyeinfoAppDelegate* appDelegate = (eyeinfoAppDelegate*)[UIApplication sharedApplication].delegate;
	GetRssDataLogical* getRssDataLogic = appDelegate.m_getITRssLogic;
	
	RssItem * anRssItem = [getRssDataLogic.m_rsslist objectAtIndex:indexPath.row];
	cell.textLabel.text = anRssItem.title;
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
	
	NewWebView * newWebViewController = [[NewWebView alloc] initWithNibName:@"NewWebView" bundle:nil];
	eyeinfoAppDelegate* appDelegate = (eyeinfoAppDelegate*)[UIApplication sharedApplication].delegate;
	GetRssDataLogical* getRssDataLogic = appDelegate.m_getITRssLogic;
	RssItem * anRssItem = [getRssDataLogic.m_rsslist objectAtIndex:indexPath.row];
	newWebViewController.m_rssItem = anRssItem;
	[[self navigationController] pushViewController:newWebViewController animated:YES];
	[newWebViewController release];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


-(IBAction) handlePageChanged
{
	[self pageChangeClick];
	[self refrashTableData:FALSE];
}

-(IBAction) refrashClick
{
	test_curl();
	[self refrashTableData:TRUE];
}

-(void) refrashTableData:(BOOL) bRefrashData
{
	[m_spinner startAnimating];
	eyeinfoAppDelegate* appDelegate = (eyeinfoAppDelegate*)[UIApplication sharedApplication].delegate;
	GetRssDataLogical* getRssDataLogic = appDelegate.m_getITRssLogic;
	if (bRefrashData == TRUE || getRssDataLogic.m_nPage != m_nCurPage)
	{
		self.navigationItem.rightBarButtonItem.title = [NSString stringWithFormat:@"刷新(%d)", m_nCurPage];
		getRssDataLogic.m_nPage = m_nCurPage;
		[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob:) toTarget:self withObject:getRssDataLogic];
	}
	else
		[self refrashTableView];

	
}


-(void) pageChangeClick
{
	switch (pageControl.selectedSegmentIndex) {
		case 0:
			if (m_nCurPage > 1)
				m_nCurPage--;
			break;
		case 1:
			m_nCurPage++;
			break;
		default:
			break;
	}
	pageControl.selectedSegmentIndex = -1;
}


- (void)startTheBackgroundJob :(GetRssDataLogical*) getRssDataLogic {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[getRssDataLogic createRssData];
    [self performSelectorOnMainThread:@selector(refrashTableView) withObject:nil waitUntilDone:NO];
    [pool release];
}




- (void)refrashTableView {
	[self.tableViewPtr reloadData];
	[m_spinner stopAnimating];
}


- (void)dealloc {
    [super dealloc];
}
@end
